package sg.techstudio.suntec.main;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sg.techstudio.suntec.dao.AdsDao;
import sg.techstudio.suntec.dao.EventContactDao;
import sg.techstudio.suntec.dao.RadiusDao;
import sg.techstudio.suntec.dao.UserDao;
import sg.techstudio.suntec.object.AdsObj;
import sg.techstudio.suntec.object.DashboardRecordObj;
import sg.techstudio.suntec.object.Radcheck;
import sg.techstudio.suntec.object.RestEventContactObj;
import sg.techstudio.suntec.object.UserObj;
import sg.techstudio.suntec.object.WifiAccountObj;

public class TestHibernate {
	private static final Log log = LogFactory.getLog(TestHibernate.class);
	private static String configLocation = "config/spring-context.xml";
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private AdsDao adsDao;
	
	@Autowired
	private RadiusDao radiusDao;
	
	@Autowired
	private EventContactDao eDao;
	
	@Autowired
	private BasicDataSource dataSource;
	
	private static TestHibernate testHibernate;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("start");
		try {
			if(args!=null && args.length>=1)
				configLocation = args[0];
			
			initProcess(configLocation);
			testHibernate.mainProcess();
			
		} catch(Exception e) {
			log.error("Exception at main():"+e, e);
		}
		System.out.println("end");
	}
	@SuppressWarnings("resource")
	private static void initProcess(String configLocation2) {
		log.debug("initProcess() entry");
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		testHibernate = ctx.getBean(TestHibernate.class);
	}

	private void mainProcess() {
		log.debug("mainProcess() entry");
//		createDummyUser();
//		createDummyAds();
//		createDummyRadcheck();
//		findEvent();
		testGroupRadcheck();
		/*
		UserObj u = new UserObj();
		u.setEmail("a@email.com");
		u.setUsername("user1");
		u.setPassword(DigestUtils.md5Hex("tss168"));
		userDao.saveUser(u);
		
		log.debug(userDao.getUser(1));
//		System.out.println("initial size:"+dataSource.getInitialSize());
//		System.out.println("max"+dataSource.getMaxActive());
		
		RoleObj r = new RoleObj();
		r.setName("role1");
		HashSet<Long> hs = new HashSet<Long>();
		hs.add(101L);
		hs.add(102L);
		r.setFunctionSet(hs);
		userDao.saveOrUpdateRole(r);
		
		u.setRole(r);
		userDao.saveUser(u);
		*/
	}
	
	
	protected void testGroupRadcheck() {
		log.debug("testGroupRadcheck() entry");
		List<DashboardRecordObj> result = radiusDao.getRadacctGroupByDay();
		for(DashboardRecordObj o: result) {
			log.debug(o+": "+o.getClass());
		}
		
		log.debug("testGroupRadcheck() exit");
	}
	protected void findEvent() {
		log.debug("findEvent() entry");
		WifiAccountObj w = new WifiAccountObj();
		w.setId(8L);
		RestEventContactObj re = eDao.findEventByWifiAccount(w);
		log.debug("found re id:"+re.getExtId());
		log.debug("findEvent() exit");
	}
	protected void createDummyRadcheck() {
		log.debug("createDummyRadcheck() entry");
		Radcheck c = new Radcheck();
		c.setUsername("u");
		c.setAttribute("MD5-Password");
		c.setOp(":=");
		c.setValue(DigestUtils.md5Hex("u3"));
		radiusDao.saveRadcheck(c);
		log.debug("createDummyRadcheck() exit");
	}
	protected void createDummyAds() {
		log.debug("createDummyArgs() entry");
		AdsObj a = new AdsObj();
		a.setName("test111");
		a.setPosition(1);
		a.setUrl("www.google.com");
		File f = new File("src/config/messages.properties");
		log.debug("130103; f exist?"+f.exists());
		
		adsDao.saveAds(a);
		log.debug("createDummyArgs() exit");
	}
	
	protected void createDummyUser() {
		log.debug("createDummyUser() entry");
		int noUser = 30;
		Set<UserObj> userSet = new HashSet<>();
		for(int i=0; i<noUser; i++) {
			UserObj user = new UserObj();
			user.setUsername("user"+RandomStringUtils.randomAlphabetic(5));
			user.setPassword(DigestUtils.md5Hex(RandomStringUtils.randomAlphabetic(5)));
			user.setEmail(RandomStringUtils.randomAlphabetic(5)+"@email.com");
			userSet.add(user);
		}
		userDao.saveUsers(userSet);
		log.debug("createDummyUser() exit");
	}
}
